﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Steamrunner
{
    class Score
    {
        #region Field

        static List<int[]> playerTimes;
        static List<short> playerPlaces;

        static short amountOfPlayers = 0;
        bool[] activePlayers;
        int totalTime = 0;

        #endregion

        #region Properties

        public List<int[]> PlayerTimes
        {
            get { return playerTimes; }
        }

        public List<short> PlayerPlaces
        {
            get { return playerPlaces; }
            set { playerPlaces = value; }
        }
        
        public short AmountOfPlayers
        {
            get { return amountOfPlayers; }
            set { amountOfPlayers = value; }
        }

        public bool[] ActivePlayers
        {
            get { return activePlayers; }
        }

        #endregion

        public Score()
        {
        }

        public void Flush(short players)
        {
            totalTime = 0;

            AmountOfPlayers = players;

            activePlayers = new bool[AmountOfPlayers];
            playerTimes = new List<int[]>(AmountOfPlayers);
            playerPlaces = new List<short>(AmountOfPlayers);
            for (int i = 0; i < AmountOfPlayers; i++)
            {
                activePlayers[i] = true;
                playerTimes.Add(new int[3] { 0, 0, 0 });
                playerPlaces.Add(4);
            }
        }

        public void DeactivatePlayer(int index)
        {
            if (index < amountOfPlayers)
            {
                activePlayers[index] = false;
            }
        }

        public void Update(GameTime gameTime)
        {
            totalTime += gameTime.ElapsedGameTime.Milliseconds;
            
            int totalMilliseconds = totalTime % 1000;
            int totalSeconds = totalTime / 1000;
            int totalMinutes = totalSeconds / 60;
            totalSeconds = totalSeconds % 60;

            for (int i = 0; i < AmountOfPlayers; i++)
            {
                if (activePlayers[i])
                {
                    playerTimes[i] = new int[3] {totalMinutes, totalSeconds, totalMilliseconds};
                }
            }
        }

        public void CalculatePlayerPlaces(List<float> characterXs)
        {
            for (int i = 0; i < AmountOfPlayers; i++)
            {
                if (activePlayers[i])
                {
                    //playerPlaces[i] = places[i];
                }
            }
        }
    }
}
